<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Usuario extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('usuario_model');
        $this->lang->load('ini_' . $this->config->item('language'), $this->config->item('language'));
        $this->lang->load('usuario_' . $this->config->item('language'), $this->config->item('language'));
        $this->lang->load('home_' . $this->config->item('language'), $this->config->item('language'));
    }
    
    public function index() {
    }
    
    public function login(){
        if($this->session->userdata('logueado')){redirect('');}
        $this->load->library('user_agent');
        $this->load->library('form_validation');
        if ($this->form_validation->run('v_login') == true) {
            $result = (array)$this->usuario_model->login($this->input->post('usu_nick'), $this->input->post('usu_pass'));
            if ($result) {
                $this->iniciar_sesion($result);
            } else {
                $this->session->set_flashdata('mensaje', lang('mensaje_login_incorrecto'));
            }
        } else {
            $this->session->set_flashdata('mensaje', lang('mensaje_login_incorrecto'));
        }
        redirect($this->agent->referrer());
    }
    
    public function registro() {
        if($this->session->userdata('logueado')){ redirect('panel'); }
        $this->load->library('form_validation');
        $this->form_validation->set_message('matches', lang('validacion_p_captcha'));
        $array = array(
            'usu_nick' => $this->input->post('usu_nick'),
            'usu_slug' => $this->general->_set_slug(slug($this->input->post('usu_nick'))),
            'usu_email' => $this->input->post('usu_email'),
            'usu_idioma' => 'spanish'
        );
        if($this->input->is_ajax_request()){//insertamos desde popup
            if ($this->form_validation->run('v_registro_mini') == true) {
                $this->load->helper('string');
                $pass = random_string('alpha', 12);
                $array['usu_pass'] = md5($pass);
                $result = (array)$this->usuario_model->registro($array);
                $this->iniciar_sesion($result);
                echo '';
            }else{
                $this->form_validation->set_error_delimiters('- ', '<br/>');
                echo validation_errors();
            }
        }else{
            if ($this->form_validation->run('v_registro') == true) {
                $array['usu_pass'] = md5($this->input->post('usu_pass'));
                $result = (array)$this->usuario_model->registro($array);
                $this->iniciar_sesion($result);

                //$this->general->envio_email_registro($result);
                redirect('panel');
            }else{
                $this->color = '009cea';
                $this->_render('registro');
            }
        }
    }

    function desconectar() {
        $array_sesiones = array('usu_id' => '', 'usu_nick' => '', 'usu_slug' => '', 'usu_desc' => '', 'usu_email' => '', 'usu_idioma' => '', 'usu_codigo_activacion' => '', 
                'usu_recordar_pass' => '', 'usu_ultimo_login' => '', 'usu_intentos_login' => '', 'usu_activo' => '', 'usu_fk_rol' => '', 'usu_avatar' => '', 'usu_feccre' => '', 
                'usu_fecmod' => '', 'usu_usucre' => '', 'usu_usumod' => '', 'usu_estado' => '');
        $this->session->unset_userdata($array_sesiones);
        $this->session->sess_destroy();
        redirect();
    }
    
    private function iniciar_sesion($result){
        $this->load->model('empresa_model');
        $this->load->library('menu');
        unset($result['usu_pass']); //no guardamos el password en sesion
        $this->session->sess_destroy();
        $this->session->sess_create();
        $this->session->set_userdata($result);
        $this->session->set_userdata('logueado', true);
        //$this->session->set_userdata('empresas', $this->empresa_model->getEmpresasUsuario(array('usu_id' => $this->session->userdata('usu_id'))));
        $this->session->set_userdata('menu', $this->menu->getMenu());
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */